home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Sample Code / Typography Samples / ShiftLayoutSample ƒ / ShiftLayoutSample.c next >
Encoding:
C/C++ Source or Header  |  1993-09-09  |  7.3 KB  |  308 lines  |  [TEXT/MPS ]

  1. /*
  2.     Manual Shift Layout Sample Program
  3.     By Eric Mader,
  4.     After Mike Fairman, Oliver Steele et. al.
  5. */
  6.  
  7. /* Copyright ©1989, 1990, 1991 Apple Computer, Inc.  All rights reserved. */
  8.  
  9.  
  10. #ifndef THINK_C
  11. #include <Quickdraw.h>
  12. #include <Fonts.h>
  13. #include <Windows.h>
  14. #include <Dialogs.h>
  15. #include <Events.h>
  16. #include <Memory.h>
  17. #include <Menus.h>
  18. #include <String.h>
  19. #include <Desk.h>
  20. #include <Script.h>
  21. #include <ToolUtils.h>
  22. #define thePort qd.thePort
  23. #define screenBits qd.screenBits
  24. #endif
  25.  
  26. #include "graphics libraries.h"
  27. #include "qd library.h"
  28. #include "graphics debugging.h"
  29. #include "graphics routines.h"
  30. #include "graphics toolbox.h"
  31.  
  32. #include "layout feature constants.h"
  33. #include "layout types.h"
  34. #include "layout routines.h"
  35. #include "layout library.h"
  36.  
  37.  
  38. /* This macro is useful for constructing fixed values */
  39. #define f(a,b) (((fixed) (a) << 16) + (b))
  40.  
  41. /* various menu and dialog things */
  42. enum {aboutDLOG = 128, appleMenuID = 128, fileMenuID, editMenuID}; /* resource ID's */
  43.  
  44. enum {okButton = 1, showsItem, authorItem};
  45.  
  46. enum {
  47.     /* Apple Menu */
  48.     aboutCommand = 1,
  49.     
  50.     /* File Menu */
  51.     quitCommand = 1,
  52.     
  53.     /* Edit Menu */
  54.     undoCommand = 1,
  55.     cutCommand,
  56.     copyCommand,
  57.     pasteCommand,
  58.     clearCommand};
  59.  
  60. WindowPtr myWindow, whichWindow;
  61. gxViewPort myPort;
  62. Rect qdWindowRect;
  63. Rect growRect = {40, 40, 32767, 32767};
  64. Rect bigRect = {-32768, -32768, 32767, 32767};
  65. gxRectangle windowRect;
  66.  
  67. MenuHandle appleMenu, fileMenu, editMenu;
  68. Boolean done = false;
  69.  
  70. gxColor colorWhite = xRGB (0xFFFF, 0xFFFF, 0xFFFF);    /* white */
  71. gxColor hsvInitial = xHSV (0, 0xFFFF, 0xFFFF); /* red */
  72.  
  73. static void ShowAboutBox ()
  74. {    GrafPtr savePort;
  75.     DialogPtr theDialog;
  76.     short itemType;
  77.     Handle itemHdl;
  78.     Rect itemRect;
  79.     short itemHit;
  80.  
  81.     GetPort(&savePort);
  82.     theDialog = GetNewDialog(aboutDLOG, nil, (WindowPtr) -1);
  83.     SetPort(theDialog);
  84.  
  85.     GetDItem(theDialog, authorItem, &itemType, &itemHdl, &itemRect);
  86.     SetIText(itemHdl, (StringPtr) "\pRice Dream");
  87.     GetDItem(theDialog, showsItem, &itemType, &itemHdl, &itemRect);
  88.     SetIText(itemHdl, (StringPtr) "\pManual Shifting");
  89.  
  90.     do {
  91.         ModalDialog(nil, &itemHit);
  92.     } while (itemHit != okButton);
  93.  
  94.     CloseDialog(theDialog);
  95.  
  96.     SetPort(savePort);
  97. }
  98.  
  99. static void SetupMenus ()
  100. {
  101.     InsertMenu (appleMenu = GetMenu (appleMenuID), 0);
  102.     AddResMenu (appleMenu, (ResType) 'DRVR');
  103.     InsertMenu (fileMenu = GetMenu (fileMenuID), 0);
  104.     InsertMenu (editMenu = GetMenu (editMenuID), 0);
  105.     DrawMenuBar ();
  106. }
  107.  
  108. static void DoMenuCommand (long mResult)
  109. {    short theItem = LoWord (mResult), theMenuID = HiWord (mResult);
  110.     GrafPtr savePort;
  111.     Str255 daName;
  112.     
  113.     switch (theMenuID)
  114.     {    case appleMenuID:
  115.             if (theItem == aboutCommand) ShowAboutBox ();
  116.             else
  117.             {    GetItem (appleMenu, theItem, daName);
  118.                 GetPort (&savePort);
  119.                 (void) OpenDeskAcc (daName);
  120.                 SetPort (savePort);
  121.             }
  122.         break;
  123.             
  124.         case fileMenuID:
  125.             if (theItem == quitCommand) done = true;
  126.         break;
  127.             
  128.         case editMenuID:
  129.         break;
  130.     }
  131.     
  132.     HiliteMenu (0);
  133. }
  134.  
  135. static short StrLength(char *s)
  136. {short len;
  137.  
  138.     for (len = 0; *s++ != 0; len++) ;
  139.     return len;
  140. }
  141.  
  142. void main()
  143. {    EventRecord theEvent;
  144.     gxShape layout, whiteOut;
  145.     gxRunControls controls;
  146.     gxLayoutOptions gxLayoutOptions;
  147.     gxStyle glyphStyles[2];
  148.     char *text1 = "Office";
  149.     char *text2 = "AWAY";
  150.     char *textRuns[2];
  151.     short level0 = 0, textLengths[2], totalLength;
  152.     gxPoint posn;
  153.     GDHandle max;
  154.     short mbh = GetMBarHeight ();
  155.     
  156.     MaxApplZone(); MoreMasters(); MoreMasters();
  157.  
  158.     SetGraphicsLibraryErrors();
  159.     SetGraphicsLibraryNotices ();
  160. /*    GXSetValidation(gxInternalValidation | gxStructureValidation | gxNoMemoryManagerValidation);    /* uncomment this for less speed and more error-checking */
  161.  
  162.     InitGraf(&thePort);
  163.     InitFonts();
  164.     InitWindows();
  165.     InitMenus ();
  166.     InitCursor();
  167.  
  168.     SetupMenus ();
  169.     
  170.     /* find the deepest monitor, and make a window that just covers it */
  171.     max = GetMaxDevice (&bigRect);
  172.     qdWindowRect = (**max).gdRect;
  173.     
  174.     /* bring it down one mbh for the header, maybe another if on main screen */
  175.     if (qdWindowRect.top == 0 && qdWindowRect.left == 0) qdWindowRect.top += mbh;
  176.     qdWindowRect.top += mbh;
  177.         
  178.     InsetRect (&qdWindowRect, 4, 4);
  179.     ShortRectToFixed (&qdWindowRect, &windowRect);
  180.     
  181.     myWindow = NewWindow(nil, &qdWindowRect, (StringPtr) "\pShift Layout Sample",
  182.              true, documentProc, (WindowPtr) -1L, true, 0L);
  183.     
  184.     myPort = GXNewWindowViewPort (myWindow);
  185.     SetDefaultViewPort (myPort);
  186.  
  187.     /*
  188.          When we want to erase the whole window, we just GXDrawShape (whiteOut).
  189.      */
  190.     whiteOut = GXNewShape (gxFullType);
  191.     GXSetShapeColor (whiteOut, &colorWhite);
  192.  
  193.     /* gxInitialize the textRuns array */
  194.     textRuns[0] = text1;
  195.     textRuns[1] = text2;
  196.     
  197.     /* gxInitialize the textLengths array */
  198.     textLengths[0] = StrLength (text1);
  199.     textLengths[1] = StrLength (text2);
  200.     
  201.     totalLength = textLengths[0] + textLengths[1];
  202.     
  203.     /* Make a default gxLayoutOptions and gxRunControls */
  204.     InitializeLayoutOptions (&gxLayoutOptions);
  205.     InitializeRunControls (&controls);
  206.     
  207.     /*
  208.         Position the layout half way down the left edge of the window. Set 
  209.         the layout's width to the window's width and set the flushness to 1/2, this
  210.         will cause the layout to center in the window.
  211.     */
  212.     gxLayoutOptions.width = windowRect.right - windowRect.left;
  213.     gxLayoutOptions.flush = fract1/2;
  214.     posn.x = 0;
  215.     posn.y = (windowRect.bottom - windowRect.top) / 2;
  216.  
  217.     /* run 0 is 60 pt. Times Roman */
  218.     glyphStyles[0] = NewLayoutStyle((char *) "\pTimes Roman", ff(60), 0, &controls, nil, 0, nil);
  219.  
  220.     /*
  221.         Set the cross stream shift to 30 to raise the text 30 points,
  222.         and the with stream shift to -4 to move the text 4 points
  223.         closer together than normal kerning would move it.
  224.     */
  225.     controls.crossStreamShift = ff(30);
  226.     controls.beforeWithStreamShift = -ff(4);
  227.  
  228.     /* run 1 is 30 pt. Times Roman */
  229.     glyphStyles[1] = NewLayoutStyle((char *) "\pTimes Roman", ff(30), 0, &controls, nil, 0, nil);
  230.     
  231.     /* Build and draw the layout. */
  232.     
  233.     layout = GXNewLayout(
  234.         2,
  235.         textLengths,
  236.         (void *) textRuns,
  237.         2,
  238.         textLengths,
  239.         glyphStyles,
  240.         1,
  241.         &totalLength,
  242.         &level0,
  243.         &gxLayoutOptions,
  244.         &posn);
  245.     GXDrawShape (layout);
  246.  
  247.     /* Now just spin in a simple event loop until it's time to go */
  248.     while (!done)
  249.     {
  250.         if (WaitNextEvent(everyEvent, &theEvent, 0, nil))
  251.         {
  252.             switch(theEvent.what)
  253.             {
  254.                 case mouseDown:
  255.                 switch (FindWindow(theEvent.where, &whichWindow))
  256.                 {    case inSysWindow:
  257.                         SystemClick(&theEvent, whichWindow);
  258.                         break;
  259.                     
  260.                     case inMenuBar:
  261.                         DoMenuCommand (MenuSelect (theEvent.where));
  262.                     break;
  263.                         
  264.                     case inDrag:
  265.                         DragWindow(whichWindow, theEvent.where, &screenBits.bounds);
  266.                     break;
  267.  
  268.                     case inGrow:
  269.                     {    register long newSize;
  270.  
  271.                         newSize = GrowWindow(whichWindow, theEvent.where, &growRect);
  272.                         SizeWindow(whichWindow, LoWord(newSize), HiWord(newSize), true);
  273.                     }
  274.                     break;
  275.                     
  276.                     case inGoAway:
  277.                         if (TrackGoAway(whichWindow, theEvent.where)) done = true;
  278.                     break;
  279.  
  280.                     case inContent:
  281.                         if (whichWindow != FrontWindow())
  282.                             SelectWindow(whichWindow);
  283.                     break;
  284.                 }
  285.                 break;
  286.  
  287.                 case keyDown:
  288.                 case autoKey:
  289.                     if (myWindow == FrontWindow () && theEvent.modifiers & cmdKey)
  290.                         DoMenuCommand (MenuKey (theEvent.message & charCodeMask));
  291.                 break;
  292.  
  293.                 case updateEvt:
  294.                     BeginUpdate((WindowPtr)theEvent.message);
  295.                     GXDrawShape (whiteOut);
  296.                     GXDrawShape (layout);
  297.                     EndUpdate((WindowPtr)theEvent.message);
  298.                 break;
  299.             }
  300.         }
  301.     }
  302.     /* dispose everything we've allocated. */
  303.     GXDisposeShape(layout);
  304.     GXDisposeStyle (glyphStyles[0]); GXDisposeStyle (glyphStyles[1]);
  305.     DisposeWindow(myWindow);
  306.     GXDisposeShape (whiteOut);
  307. }
  308.